const app = getApp();
const { AskComponent } = app._require("common/index");
const { downloadFile } = app._require("utils/download.file");
// 列表视频播放处理
// https://developers.weixin.qq.com/community/develop/doc/000e4ef22583d8961919efb6b56009
AskComponent({
    properties: {
        list: {
            type: Array,
            value: [],
        },
        current: {
            type: Number,
            value: 0,
        },
    },
    data: {
        currentDuration: 0,
        display: false,
        transitionEnd: false,
        status: "",
    },
    methods: {
        handelCloseClick() {
            this.close();
        },
        handelDownloadClick() {
            wx.showLoading({
                title: "下载中...",
                mask: true,
            });
            const item = this.data.list[this.data.current];
            const _url = item.src || item.filePath || item;
            downloadFile({
                url: _url,
                fail: res => {
                    wx.showModal({
                        title: "下载失败",
                        content: _url,
                        cancelText: "关闭",
                        confirmText: "复制",
                        success: res => {
                            if (res.confirm) {
                                wx.setClipboardData({
                                    data: _url,
                                    success(res) {
                                        console.log(res);
                                    },
                                });
                            }
                        },
                    });
                    console.log(res);
                },
                complete: () => {
                    wx.hideLoading();
                },
            });
        },
        open(current) {
            if (current !== undefined) this.setData({ current });
            this._openModal();
        },
        close() {
            this._closeModal();
        },
        _openModal() {
            this.setData({
                display: true,
            });
        },
        _closeModal() {
            this.selectComponent("#popup").onClickCloseIcon();
        },
        onPopupOpen() {
            console.log("open");
            this.setData({
                display: true,
            });
        },
        onPopupClosed() {
            this.setData({
                display: false,
            });
        },
        videoErrorCallback(error) {
            console.log("error", error);
            wx.askShowToastEx({
                icon: "none",
                title: "视频播放异常",
            });
        },
    },
});
